package com.xx.system.service.impl;

import com.xx.common.utils.StringUtils;
import com.xx.system.domain.SysUserOnline;
import com.xx.system.service.ISysUserOnlineService;
import org.springframework.stereotype.Service;

/**
 * 在线用户 服务层处理
 *
 * @author xx
 */
@Service
public class SysUserOnlineServiceImpl implements ISysUserOnlineService {
    /**
     * 通过登录地址查询信息
     *
     * @return 在线用户信息
     */
    @Override
    public SysUserOnline selectOnlineByIpaddr(String ipaddr, String tokenId,
                                              String username,
                                              String ipaddrS,
                                              String loginLocation, String browser,
                                              String os, Long loginTime,
                                              String deptName) {
        if (StringUtils.equals(ipaddr, ipaddrS)) {
            return loginUserToUserOnline(tokenId, username, ipaddr, loginLocation, browser, os, loginTime, deptName);
        }
        return null;
    }

    /**
     * 通过用户名称查询信息
     *
     * @return 在线用户信息
     */
    @Override
    public SysUserOnline selectOnlineByUserName(String userName, String tokenId,
                                                String username,
                                                String ipaddr,
                                                String loginLocation, String browser,
                                                String os, Long loginTime,
                                                String deptName) {
        if (StringUtils.equals(userName, username)) {
            return loginUserToUserOnline(tokenId, username, ipaddr, loginLocation, browser, os, loginTime, deptName);
        }
        return null;
    }

    /**
     * 通过登录地址/用户名称查询信息
     *
     * @return 在线用户信息
     */
    @Override
    public SysUserOnline selectOnlineByInfo(String ipaddr, String userName, String tokenId,
                                            String username,
                                            String ipaddrS,
                                            String loginLocation, String browser,
                                            String os, Long loginTime,
                                            String deptName) {
        if (StringUtils.equals(ipaddr, ipaddrS) && StringUtils.equals(userName, username)) {
            return loginUserToUserOnline(tokenId, username, ipaddrS, loginLocation, browser, os, loginTime, deptName);
        }
        return null;
    }

    /**
     * 设置在线用户信息
     *
     * @return 在线用户
     */
    @Override
    public SysUserOnline loginUserToUserOnline(String tokenId,
                                               String username,
                                               String ipaddr,
                                               String loginLocation, String browser,
                                               String os, Long loginTime,
                                               String deptName) {

        SysUserOnline sysUserOnline = new SysUserOnline();
        sysUserOnline.setTokenId(tokenId);
        sysUserOnline.setUserName(username);
        sysUserOnline.setIpaddr(ipaddr);
        sysUserOnline.setLoginLocation(loginLocation);
        sysUserOnline.setBrowser(browser);
        sysUserOnline.setOs(os);
        sysUserOnline.setLoginTime(loginTime);
        if (StringUtils.isNotNull(deptName)) {
            sysUserOnline.setDeptName(deptName);
        }
        return sysUserOnline;
    }
}
